package com.bvgol.example.rabbitmq;/**
 * @Classname SendAndReceive
 * @Description TODO
 * @Date 2021/4/28 15:12
 * @Created by GUOCHEN
 */

import com.bvgol.example.rabbitmq.config.RabbitMqConfig;
import com.bvgol.example.rabbitmq.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.UUID;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2021/04/28 15:12
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SendAndReceive {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @see RabbitMqConfig#messageConverter()
     */
    @Test
    public void sendMessageTest() {
        OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
        reasonEntity.setId(1L);
        reasonEntity.setCreateTime(new Date());
        reasonEntity.setName("reason");
        reasonEntity.setStatus(1);
        reasonEntity.setSort(2);
        String msg = "Hello World";
        //1、发送消息,如果发送的消息是个对象，会使用序列化机制，将对象写出去，对象必须实现Serializable接口

        //2、发送的对象类型的消息，可以是一个json
        rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java",
                reasonEntity, new CorrelationData(UUID.randomUUID().toString()));
        log.info("消息发送完成:{}", reasonEntity);
    }

}
